package com.sumainfor.pc.gmio.service.impl;

import com.sumainfor.common.Constants;
import com.sumainfor.common.entity.ParentDeptVo;
import com.sumainfor.common.entity.SysUserVo;
import com.sumainfor.common.util.ContextUtils;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.ComUtils;
import com.sumainfor.common.utlis.ParamsCommon;
import com.sumainfor.pc.gmio.dao.HoForcedMapper;
import com.sumainfor.pc.gmio.dao.HoKeepTenantMapper;
import com.sumainfor.pc.gmio.dao.UtilsMapper;
import com.sumainfor.pc.gmio.feign.Zfeign.ZpcMktFeign;
import com.sumainfor.pc.gmio.feign.Zfeign.ZzuulFeign;
import com.sumainfor.pc.gmio.service.HoForcedService;
import com.sumainfor.pc.gmio.service.HoKeepTenantService;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 服务管家强制退
 */
@Service
public class HoKeepTenantSerivceImpl implements HoKeepTenantService {

    private ReentrantLock lock = new ReentrantLock(false);
    Logger log = LoggerFactory.getLogger(HoKeepTenantSerivceImpl.class);
    @Autowired
    private HoKeepTenantMapper hoKeepTenantMapper;
    @Autowired
    private ZzuulFeign zzuulFeign;
    @Autowired
    private ZpcMktFeign zpcMktFeign;

    @Override
    public List<Map<String, Object>> getHoKeepTenantList(Map<String, Object> params) {
        List<Map<String,Object>> gmioList=hoKeepTenantMapper.getHoKeepTenantList(params);
        List<String> sysUserList=new ArrayList<String>();
        List<String> TenantList=new ArrayList<String>();
        for (Map<String,Object> map: gmioList){
            if(map.get("SysUserId")!=null){
                sysUserList.add(map.get("SysUserId").toString());
            }
            if(map.get("TenId")!=null){
                TenantList.add(map.get("TenId").toString());
            }
        }
        params.put("sysUserList",sysUserList);
        //查询管家的信息
        JsonResult zuulResutl=zzuulFeign.getReapirSysUser(params);
        if (zuulResutl.get("errorCode").toString().equals("0")){
            List<Map<String,Object>> zuulList=(List<Map<String,Object>>)zuulResutl.get("result");
            for (Map<String,Object> map1:zuulList){
                if (map1.get("SysUserID")!=null){
                    for (Map<String,Object> map2:gmioList){
                        if (map2.get("SysUserId")!=null){
                            if (map1.get("SysUserID").toString().equals(map2.get("SysUserId").toString())){
                                map2.putAll(map1);
                            }
                        }
                    }
                }

            }
        }
        //到市场部查询评价租客的信息
        params.put("TenantList",TenantList);
        JsonResult marketResult=zpcMktFeign.getTenantListByTenId(params);
        if (zuulResutl.get("errorCode").toString().equals("0")){
            List<Map<String,Object>> marketList=(List<Map<String,Object>>)marketResult.get("result");
            for (Map<String,Object> map1:marketList){
                if (map1.get("TenId")!=null){
                    for (Map<String,Object> map2:gmioList){
                        if (map2.get("TenId")!=null){
                            if (map1.get("TenId").toString().equals(map2.get("TenId").toString())){
                                map2.putAll(map1);
                            }
                        }
                    }
                }
            }
        }
        return gmioList;
    }

    @Override
    public Integer getHoKeepTenantListCount(Map<String, Object> params) {
        return hoKeepTenantMapper.getHoKeepTenantListCount(params);
    }
}
